#define MAX_LISTEN 1024
#include"Log.hpp"
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<signal.h>
class NetWort{
public:
    NetWort(){
        lg(Info,"SIGPIPE INIT");
        signal(SIGPIPE,SIG_IGN);
    }
};

static NetWort nw;

class Socket{
private:
    int _sockfd;
public:
    Socket():_sockfd(-1) {}
    Socket(int fd): _sockfd(fd) {}
    ~Socket() { Close(); }

    int Fd() { return _sockfd; }
    //创建套接字
    bool Create(){
        _sockfd = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
        if(_sockfd < 0){
            lg(Error,"CREATE SOCKET FAILED!!");
            return false;
        }
        return false;
    }
    //bind address
    bool Bind(const std::string &ip, uint16_t port){
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port); 
        addr.sin_addr.s_addr = inet_addr(ip.c_str());
        socklen_t len = sizeof(struct sockaddr_in);
        int ret = bind(_sockfd, (struct sockaddr*)&addr,len);
        if(ret<0){
            lg(Error,"BIND ADDRESS FAILED!");
            return false;
        }
        return true;
    }
    bool Listen(int backlog = MAX_LISTEN){ //设置监听
        int ret = listen(_sockfd,backlog);        
        if(ret<0){
            lg(Error,"SOCKET LISTEN FAILED!");
            return false;
        }
        return true;
    }
    //向服务器发起连接
    bool Connect(const std::string &ip, uint16_t port){
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = inet_addr(ip.c_str());
        socklen_t len = sizeof(struct sockaddr_in);
        int ret = connect(_sockfd, (struct sockaddr *)&addr, len);
        if(ret <0){
            lg(Error, "CONNECT SERVER FAILED!");
            return false;
        }
        return true;
    }
    //获取新连接
    int Accept(){
        int newfd = accept(_sockfd,NULL,NULL);
        if(newfd < 0){
            lg(Error, "SCOKET ACCEPT FAOLED!");
            return -1;
        }
        return newfd;
    }
    //接受数据
    ssize_t Recv(void *buf, size_t len, int flag = 0){
        ssize_t  ret = recv(_sockfd, buf, len, flag);
        if(ret <= 0){
            if(errno == EAGAIN || errno == EINTR){
                return 0;
            }
            lg(Error,"SOCKET RECV FAILED!");
            return -1;
        }
        return ret;
    }
    ssize_t NonBlockRecv(void *buf, size_t len){
        return Recv(buf,len,MSG_DONTWAIT); //MSG_DONTWAIT设置成非阻塞
    }
    //发送数据
    ssize_t Send(const void *buf, size_t len, int flag = 0){
        ssize_t  ret = send(_sockfd,buf,len,flag);
        if(ret<0){
            if(errno == EAGAIN || errno == EINTR){
                return 0;
            }
            lg(Error,"SOCKET SEND FAILED!");
            return -1;
        }
        return ret;
    }

    ssize_t NonBlockSend(void *buf, size_t len){
        if(len == 0 ) return 0;
        return Send(buf, len, MSG_DONTWAIT);
    }

    void Close(){
        if(_sockfd != -1){
            close(_sockfd);
            _sockfd=-1;
        }
    }

    bool CreateServer(uint16_t port,bool block_flag = false ,const std::string &ip = "0.0.0.0"){
        if(Create()==false) return false;
        if(block_flag) NonBlock();
        if(Bind(ip,port) == false) return false;
        if(Listen() == false) return false;
        ReuseAddress();
        return true;
    }

    bool CreateClient(uint16_t port, const std::string &ip){
        if(Create() == false) return false;
        if(Connect(ip,port) == false) return false;
        return true;
    }
    void ReuseAddress(){
        int val=1;
        setsockopt(_sockfd,SOL_SOCKET,SO_REUSEADDR,(void *)&val,sizeof(int));
        val=1;
        setsockopt(_sockfd,SOL_SOCKET,SO_REUSEPORT,(void*)&val,sizeof(int));
    }
    void NonBlock(){
        int flag = fcntl(_sockfd,F_GETFL,0);
        fcntl(_sockfd,F_SETFL,flag | O_NONBLOCK);
    }
};

// int main(){
//     Socket server;
//     server.CreateServer(8888,false);
//     return 0;
// }